package main.java.view;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import main.java.model.Lights;

public class LightsView extends JPanel implements Observer {
    
    //List of colors
    private static final String [] COLORS = {"White", "Green", "Orange", "Red"};
    
    //static final variables to change positions id easier
    private static final int WHITE_ID = 0;
    private static final int GREEN_ID = 1;
    private static final int ORANGE_ID = 2;
    private static final int RED_ID = 3;
    
    //static final variables of view dimension to modify them easier
    private static final int SIZE1 = 500;
    
    //Path of each image of the lights
    private static String lightsWhiteImage = "images/LightsWhite.png";
    private static String lightsGreenImage = "images/LightsGreen.png";
    private static String lightsOrangeImage = "images/LightsOrange.png";
    private static String lightsRedImage = "images/LightsRed.png";
    
    private Image lightsImage;

    /**
     * Constructor LightsView
     */
    public LightsView() {
        //Set to white
        this.lightsImage = new ImageIcon(lightsWhiteImage).getImage(); 
        
        //Initialize panel info
        Dimension size = new Dimension(lightsImage.getWidth(null), lightsImage.getHeight(null));
        this.setPreferredSize(size);
        this.setMinimumSize(size);
        this.setMaximumSize(size);
        this.setSize(size);
        this.setLayout(null);
    }

    /**
     * Switch the image depending on the position of the lights
     * @param position : position of the lights
     */
    public void switchImage(String position) {
        switch (position) {
            //If White then set lightsWhiteImage
            case "White": 
                this.lightsImage = new ImageIcon(lightsWhiteImage).getImage();
                break;
            //If Green then set lightsGreenImage
            case "Green":
                this.lightsImage = new ImageIcon(lightsGreenImage).getImage();
                break;
            //If Orange then set lightsOrangeImage
            case "Orange": 
                this.lightsImage = new ImageIcon(lightsOrangeImage).getImage();
                break;
            //If Red then set lightsRedImage
            case "Red": 
                this.lightsImage = new ImageIcon(lightsRedImage).getImage();
                break;
            default:
                break;
        }
        //repaint
        paintImmediately(0, 0, SIZE1, SIZE1); 
    }

    /**
     * Draw image
     */
    public void paintComponent(Graphics g) {
        g.drawImage(lightsImage, 0, 0, null);
    }
    
    /**
     * Update image 
     * Depending on the position of the lights
     * @param Observable : what type of instance
     * @param Object : given change 
     */
    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof Lights) {
            switch (arg.toString()) {
                case "White":
                    switchImage(COLORS[WHITE_ID]);
                    break;
                case "Green":
                    switchImage(COLORS[GREEN_ID]);
                    break;
                case "Orange":
                    switchImage(COLORS[ORANGE_ID]);
                    break;
                case "Red":
                    switchImage(COLORS[RED_ID]);
                    break;
                default:
                    break;
            }
        }
    }
}
